package util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class FileUtil {

	/***
	 * Get the content of the file assigned by the file path
	 * 
	 * @param filePath
	 * @return
	 */
	public static String getFileContentFromFilePath(String filePath) {
		File file = new File(filePath);
		BufferedReader reader = null;
		ArrayList xmlText = null;
		String line;
		StringBuffer text = new StringBuffer();
		xmlText = new ArrayList();
		try {
			reader = new BufferedReader(new FileReader(file));
			while ((line = reader.readLine()) != null) {
				text.append(line + "\n");
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text.toString();
	}

	/***
	 * Get file from the assigned file path
	 * 
	 * @param filePath
	 * @return
	 */
	public static File getFileFromFilePath(String filePath) {
		return new File(filePath);
	}

	/***
	 * Get file path from the opening up dialog
	 * 
	 * @param shell
	 * @param filterNames
	 * @param filterExtensions
	 * @return
	 */
	public static String getFilePathFromDlg(Shell shell, String[] filterNames,
			String[] filterExtensions,String filterPath, int openOrSave) {
		FileDialog dialog;
		if(openOrSave == 0)
			dialog= new FileDialog(shell, SWT.OPEN);
		else
			dialog= new FileDialog(shell, SWT.SAVE);
		if("".equals(filterPath))
			dialog.setFilterPath(new File(Platform.getInstanceLocation().getURL().getPath()).getAbsolutePath());
		else 
			dialog.setFilterPath(filterPath);
		dialog.setFilterNames(filterNames);
		dialog.setFilterExtensions(filterExtensions);
		return dialog.open();
	}

	/**
	 * 
	 * @param filePath
	 * @param paraKey
	 * @param paraValue
	 */
	public static void writeProperties(String filePath, String paraKey,
			String paraValue) {
		Properties props = new Properties();
		try {
			OutputStream ops = new FileOutputStream(filePath);
			props.setProperty(paraKey, paraValue);
			props.store(ops, "set");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param filePath
	 * @param key
	 * @return
	 */
	public static String readValue(String filePath, String key) {
		Properties props = new Properties();
		try {
			InputStream ips = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(ips);
			String value = props.getProperty(key);
//			System.out.println(key + "=" + value);
			return value;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	// 读取全部信息
	/**
	 * 
	 */
	public static void readProperties(String filePath) {
		Properties props = new Properties();
		try {
			InputStream ips = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(ips);
			Enumeration enums = props.propertyNames();
			while (enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				String value = props.getProperty(key);
//				System.out.println(key + "=" + value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/***
	 * 
	 * @param path
	 * @param content
	 */
	public static void write(String path, String content) {
	      String s = new String();
	      String s1 = new String();
	      try {
	       File f = new File(path);
	       if (f.exists()) {
	        System.out.println("文件存在");
	       } else {
	        System.out.println("文件不存在，正在创建...");
	        if (f.createNewFile()) {
	         System.out.println("文件创建成功！");
	        } else {
	         System.out.println("文件创建失败！");
	        }

	       }
	       BufferedReader input = new BufferedReader(new FileReader(f));

	       while ((s = input.readLine()) != null) {
	        s1 += s + "\n";
	       }
	       System.out.println("文件内容：" + s1);
	       input.close();
	       s1 += content;

	       BufferedWriter output = new BufferedWriter(new FileWriter(f));
	       output.write(s1);
	       output.close();
	      } catch (Exception e) {
	       e.printStackTrace();
	      }
	}
	
	/***
	 * 
	 * @param o, object which is defined to write in s
	 * @param s, string presents the file to be written in 
	 */
	public static void writeObject(Object o, String s) {
		ObjectOutputStream out;
		try {
			out = new ObjectOutputStream(new FileOutputStream(s));
			out.writeObject(o);
			 out.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	public static Object readObject(String s) {
		if(new File(s).length()==0)
			return null;
		  ObjectInputStream in;
		  Object o = null;
		  try {
			in = new ObjectInputStream(
			            new FileInputStream(s));
			o = in.readObject();
			in.close();
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		return o;
	}
}
